/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ch.repit.aglaja.client.contacts;

import ch.repit.rwt.client.Bento;
import ch.repit.rwt.client.BentoDef;
import ch.repit.rwt.client.BentoDefFactory;
import ch.repit.rwt.client.BentoStatus;
import ch.repit.rwt.client.ObjectRef;
import ch.repit.rwt.client.audit.AuditListPage;
import ch.repit.rwt.client.audit.AuditQueryDef;
import ch.repit.rwt.client.persistence.CacheEvent;
import ch.repit.rwt.client.persistence.CacheManager;
import ch.repit.rwt.client.security.Action;
import ch.repit.rwt.client.security.Authorizer;
import ch.repit.rwt.client.ui.BentoPage;
import ch.repit.rwt.client.security.SecurityManager;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tc149752
 */
public class ContactDetailsPage extends BentoPage {

    private ObjectRef person2show = null;

    private ObjectRef addressRef = null;

    /**
     *
     * @param oRef can be either an address ref or a person ref. In the latter case,
     */
    public ContactDetailsPage(ObjectRef oRef)
    {
        super(AddressDef.TYPE, oRef.getType().equals(AddressDef.TYPE)?oRef:new ObjectRef(CacheManager.get().getCachedObject(oRef).get(PersonDef.ATTR_ADDRESSREF).getString()), null);
        if (oRef.getType().equals(PersonDef.TYPE)) {
            person2show = oRef;
            addressRef = new ObjectRef(CacheManager.get().getCachedObject(oRef).get(PersonDef.ATTR_ADDRESSREF).getString());
        }
        else {
            addressRef = oRef;
        }
    }


    
    @Override
    protected void init() {

        BentoDef personDef = BentoDefFactory.get().getDef(PersonDef.TYPE);
        //ObjectRef addressRef = super.getObject().getRef();
        Bento address = CacheManager.get().getCachedObject(addressRef);
        if (address != null) 
            setTitle(address.getDisplayValue(AddressDef.ATTR_HEADER));

        addTab("Adresse", new AddressTab(addressRef, this));

        // look for persons with this address
        List<Bento> householdPersons = new ArrayList();
        for (Bento b : CacheManager.get().getCachedObjects(PersonDef.TYPE))
            if ( new ObjectRef(b.get(PersonDef.ATTR_ADDRESSREF).getString()).equals(addressRef) 
                    && (address == null || b.getStatus().equals(address.getStatus())) )
                householdPersons.add(b);

        if (address != null) {
            boolean onlyFirstnames = address.get(AddressDef.ATTR_DISPLAYNAMES).getBoolean()==Boolean.FALSE;
            for (Bento person : householdPersons) {
                addTab(onlyFirstnames?person.getDisplayValue(PersonDef.ATTR_FIRSTNAME):personDef.getCommonName(person),
                        new PersonTab(person.getRef(), this),
                        person.getRef().equals(person2show) );
            }
        }

        // TBD: modify AuditListPage to run several audit queries (in parallel?)
        //      and display the results (one for address, and one per user)
        if (!isCreate()) {
            Authorizer auth = SecurityManager.get().getAuthorizer();

            if (auth.isAllowed(Action.AUDIT, getObject())) {
                Bento[] queries = new Bento[householdPersons.size() + 1];
                int i = 0;
                Bento queryAddress = BentoDefFactory.get().getDef(AuditQueryDef.TYPE).createBento();
                queryAddress.get(AuditQueryDef.ATTR_OBJECTREF).set(getObject().getRef().toString());
                queryAddress.get(AuditQueryDef.ATTR_FROMDAYS).set(90);
                queries[i++] = queryAddress;
                for (Bento p : householdPersons) {
                    Bento queryPerson = BentoDefFactory.get().getDef(AuditQueryDef.TYPE).createBento();
                    queryPerson.get(AuditQueryDef.ATTR_OBJECTREF).set(p.getRef().toString());
                    queryPerson.get(AuditQueryDef.ATTR_FROMDAYS).set(90);
                    queries[i++] = queryPerson;
                }
                super.addTab("Historique", new AuditListPage(this, queries));
            }
        }

        // set person if linked from person
        // TBD: would require an extension of Page class, in the doLayout method 

        super.init();
    }

    
    @Override
    public void onCacheEvent(CacheEvent event) {
        super.onCacheEvent(event);

        boolean concerned = false;
        for (ObjectRef oref : event.getConcernedObjects()) {
            if (oref.equals(getObject().getRef())) {
                concerned = true;
                break;
            } else if (oref.getType().equals(PersonDef.TYPE)
                    && CacheManager.get().getCachedObject(oref).getDisplayValue(PersonDef.ATTR_ADDRESSREF)
                        .equals(getObject().getRef().toString()) ) {
                concerned = true;
                break;
            }
        }

        if (concerned) {
            getPageNav().back();
            getPageNav().displayPage(new ContactDetailsPage(getObject().getRef()));
        }

    }



}
